<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
        <a-form-item label="積分名稱" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['points_name', { rules: [{ required: true, message: '請輸入積分名稱' }] }]"
          />
          <div class="form-item-help">
            <small>注：商家使用自定義的積分名稱來做品牌運營。 如京東把積分稱為“京豆”，淘寶把積分稱為“淘金幣”</small>
          </div>
        </a-form-item>
        <a-form-item label="積分說明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            :autoSize="{ minRows: 4 }"
            v-decorator="['describe', { rules: [{ required: true, message: '請輸入積分說明' }] }]"
          />
        </a-form-item>

        <a-divider orientation="left">積分贈送</a-divider>
        <a-form-item label="購物送積分" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['is_shopping_gift', { initialValue: 1, rules: [{ required: true }] }]"
          >
            <a-radio :value="1">開啟</a-radio>
            <a-radio :value="0">關閉</a-radio>
          </a-radio-group>
          <div class="form-item-help">
            <small>積分贈送規則：1.訂單確認收貨已完成； 2.已完成訂單超出後臺設定的申請售後期限</small>
          </div>
        </a-form-item>
        <div v-show="form.getFieldValue('is_shopping_gift') == 1">
          <a-form-item label="積分贈送比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input-number
              :min="0.1"
              :max="100"
              :precision="1"
              v-decorator="['gift_ratio', { rules: [{ required: true, message: '請輸入積分贈送比例' }] }]"
            />
            <span class="ml-10">%</span>
            <div class="form-item-help">
              <p class="extra">注：贈送比例請填寫數位0~100； 訂單的運費不參與積分贈送</p>
              <p class="extra">例：訂單付款金額（100.00元）*積分贈送比例（100%）=實際贈送的積分（100積分）</p>
            </div>
          </a-form-item>
        </div>

        <a-divider orientation="left">積分抵扣</a-divider>
        <a-form-item label="下單使用積分抵扣" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['is_shopping_discount', { initialValue: 1, rules: [{ required: true }] }]"
          >
            <a-radio :value="1">開啟</a-radio>
            <a-radio :value="0">關閉</a-radio>
          </a-radio-group>
          <div class="form-item-help">
            <small>注：如開啟則用戶下單時可選擇使用積分抵扣訂單金額</small>
          </div>
        </a-form-item>

        <div v-show="form.getFieldValue('is_shopping_discount') == 1">
          <a-form-item label="積分抵扣比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <InputNumberGroup
              addonBefore="1個積分可抵扣"
              addonAfter="元"
              :inputProps="{ min: 0.01, precision: 2 }"
              v-decorator="['discount.discount_ratio', { rules: [{ required: true, message: '積分抵扣比例不能為空' }] }]"
            />
            <div class="form-item-help">
              <small>例如：設定1個積分可抵扣0.1元，則10積分可抵扣1元，100積分可抵扣10元</small>
            </div>
          </a-form-item>
          <a-form-item label="最高可抵扣金額" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-form-item>
              <InputNumberGroup
                addonBefore="訂單滿"
                addonAfter="元"
                :inputProps="{ min: 0.01, precision: 2 }"
                v-decorator="['discount.full_order_price', { rules: [{ required: true, message: '抵扣條件不能為空' }] }]"
              />
            </a-form-item>
            <a-form-item>
              <InputNumberGroup
                addonBefore="最高可抵扣金額"
                addonAfter="%"
                :inputProps="{ min: 0.1, max: 90, precision: 1 }"
                v-decorator="['discount.max_money_ratio', { rules: [{ required: true, message: '最高可抵扣金額不能為空' }] }]"
              />
              <div class="form-item-help">
                <small>例如：設定最高可抵扣10%，訂單金額100元，此時用戶可抵扣10元</small>
              </div>
            </a-form-item>
          </a-form-item>
        </div>
        <a-form-item :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/setting/store'
import { InputNumberGroup } from '@/components'

export default {
  components: {
    InputNumberGroup
  },
  data () {
    return {
      // 当前设置项的key
      key: 'points',
      // 标签布局属性
      labelCol: { span: 4 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录详情
      record: {}
    }
  },
  // 初始化数据
  created () {
    // 获取当前详情记录
    this.getDetail()
  },
  methods: {

    // 获取当前详情记录
    getDetail () {
      this.isLoading = true
      Api.detail(this.key)
        .then(result => {
          // 当前记录
          this.record = result.data.values
          // 设置默认值
          this.setFieldsValue()
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, $nextTick, form: { setFieldsValue } } = this
      $nextTick(() => {
        setFieldsValue(pick(record, [
          'points_name', 'describe',
          'is_shopping_gift', 'gift_ratio',
          'is_shopping_discount', 'discount'
        ]))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.isLoading = true
      Api.update(this.key, { form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5)
        })
        .finally((result) => {
          this.isLoading = false
        })
    }

  }
}
</script>
<style lang="less" scoped>
.ant-form-item {
  .ant-form-item {
    margin-bottom: 0;
  }
}

/deep/.ant-form-item-control {
  padding-left: 10px;
  .ant-form-item-control {
    padding-left: 0;
  }
}
</style>
